<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04_员工列表练习</title>
</head>
<body>
<input type="text" id="i1" placeholder="请输入员工姓名" >
<input type="text" id="i2" placeholder="请输入员工薪资" >
<input type="text" id="i3" placeholder="请输入员工岗位"  >
<button onclick="addEmp()">点我添加</button>
<hr>
<table border="1">
    <caption>员工列表</caption>
    <tr>
        <th >员工姓名</th>
        <th >员工薪资</th>
        <th >员工岗位</th>
    </tr>
</table>
<script>
    let arr = [];
    function addEmp(){
        let uName = document.getElementById('i1').value
        let uSalary = document.getElementById('i2').value
        let uJob = document.getElementById('i3').value
        //3.对数据进行校验
        if(uName.trim()=='' || uSalary.trim()=='' || uJob.trim()==''){
            alert('请输入完整的员工信息!');
            return;
        }
        //4.每多一个员工信息,就要创建1个tr与3个td
        let trE = document.createElement('tr');
        let nameTd = document.createElement('td');
        let salaryTd = document.createElement('td');
        let jobTd = document.createElement('td');
        //5.给创建好的td赋值
        nameTd.innerHTML = uName;
        salaryTd.innerHTML = uSalary;
        jobTd.innerHTML = uJob;
        //6.将准备好的三个td添加到tr中
        trE.append(nameTd,salaryTd,jobTd);
        //7.获取显示tr的表格元素
        let tableE = document.querySelector('table');
        //8.将tr添加到表格中
        tableE.append(trE);
        //9.清空输入框
        document.getElementById('i1').value = '';
        document.getElementById('i2').value = '';
        document.getElementById('i3').value = '';

        arr.push({
            name:uName,
            salary:uSalary,
            job:uJob
        })

    }
</script>

</body>
</html>